import {
    ArgumentsHost,
    Catch,
    ExceptionFilter,
    HttpException,
} from '@nestjs/common';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter<HttpException> {
    catch(exception: HttpException, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse();
        const request = ctx.getRequest();
        const status = exception.getStatus();
        console.error(exception)
        const exceptionRes: any = exception.getResponse();
        let message: string;
        if (typeof exceptionRes === 'string') {
            message = exceptionRes
        } else {
            message = exceptionRes.message;
        }

        response.status(status).json({
            status,
            timestamp: new Date().toISOString(),
            path: request.url,
            message,
        });
    }
}
